package com.hxzy.controller.api.system;


import cn.hutool.core.util.StrUtil;
import com.hxzy.common.util.DictDataUtil;
import com.hxzy.common.vo.CodeEnum;
import com.hxzy.common.vo.R;
import com.hxzy.dto.system.DictDataDTO;
import com.hxzy.entity.system.SysDictData;
import com.hxzy.service.system.SysDictDataService;
import com.hxzy.vo.system.HxzyUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Api(tags = "字典数据")
@RestController
@RequestMapping(value = "/api")
public class DictDataController {

    @Autowired
    private SysDictDataService dictDataService;

    @Autowired
    private DictDataUtil  dictDataUtil;


    @ApiOperation(value = "分页查询")
    @PostMapping(value = "/dictData/getData")
    public R selectAll(@RequestBody DictDataDTO dictTypeDTO){
        return R.success(this.dictDataService.selectAll(dictTypeDTO));
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping(value = "/dictData/findData")
    public R findData(Long id){
        SysDictData data = this.dictDataService.findData(id);
        if (data.getListClass().equalsIgnoreCase("")){
            data.setListClass("default");
        }
        return R.success(data);
    }

    @ApiOperation(value = "新增或者修改字典数据")
    @PostMapping(value = "/dictData/addORupdateData")
    public R addData(@RequestBody SysDictData sysDictData){
        // 从redis中取出当前用户信息
        HxzyUser hxzyUser= (HxzyUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = hxzyUser.getSysUser().getName();
        if (sysDictData.getListClass().equalsIgnoreCase("default")){
            sysDictData.setListClass("");
        }
        if (sysDictData.getId()!=null){
            sysDictData.setUpdateBy(name);
            sysDictData.setUpdateTime(new Date());
            this.dictDataService.updateById(sysDictData);

        }else {
            sysDictData.setCreateBy(name);
            sysDictData.setCreateTime(new Date());
            this.dictDataService.save(sysDictData);
        }

        // 同步缓存
        this.dictDataUtil.refreshCacheByDictTypeName(sysDictData.getDictType());

        return R.success();
    }

    @ApiOperation(value = "根据id删除")
    @PostMapping(value = "/dictData/deleteData")
    public R deleteData(@RequestBody Long[] ids){
        List<Long> list=new ArrayList<>(Arrays.asList(ids));
        return R.success(this.dictDataService.removeBatchByIds(list));
    }


    @ApiOperation(value = "类型数据")
    @GetMapping(value = "/dictData/getDictType")
    public R select(){
        return R.success(this.dictDataService.selectDictType());

    }
    @ApiOperation(value = "回显样式数据")
    @GetMapping(value = "/dictData/getClassList")
    public R selectClass(){
        return R.success(this.dictDataService.selectClass());
    }


    @ApiOperation(value = "根据dict_type查询值")
    @GetMapping(value = "/dictData/getDictTypeList/{name}")
    public R selectByDictType(@PathVariable(value = "name") String dictType){
        if(StrUtil.isBlank(dictType)){
            return R.failed(CodeEnum.DATA_VALIDATED);
        }

        List<SysDictData> dataByDictType = this.dictDataUtil.getDataByDictType(dictType);
        return R.success(dataByDictType);
    }


}
